package com.unicorn.condition;

import com.unicorn.annotation.MyConditionOnClass;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @Description
 * @Author Meng.shen
 * @Date 2022/12/6
 **/
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Map<String, Object> attributes = metadata.getAnnotationAttributes(MyConditionOnClass.class.getName());
        //org.apache.catalina.startup.Tomcat org.eclipse.jetty.server.Server
        String value = (String) attributes.get("value");
        try {
            context.getClassLoader().loadClass(value);
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}
